/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F288
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include	"SN32F280.h"
#include	"SN32F200_Def.h"
#include  "D8563.h"
#include  "LCD.h"
#include  "LCD_APP.h"
#include  "..\Driver\Utility.h"
#include  "..\Driver\I2C.h"
#include  "..\KB\KB_Scan.h"
/*_____ D E C L A R A T I O N S ____________________________________________*/


/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/
/*****************************************************************************
* Function		: D8563_Read
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void D8563_Read(void)
{
	if(rSysLCD_Mode != LCD_NORMAL_MODE)
		return;
	RD_I2C_NByte_Val(D8563_ADDR,0x02,7);
	
	if(hwI2C_Fifo[0] != BCD_Time.Second)
	{
		BCD_Time.Second = hwI2C_Fifo[0];
		RTC_Time.Second=((BCD_Time.Second>>4)&0x07)*10+(BCD_Time.Second&0x0F);
		LCD_WTAppFifo(LCD_SECOND);
	}
	
	if(hwI2C_Fifo[1] != BCD_Time.Minute)
	{
		BCD_Time.Minute = hwI2C_Fifo[1];
		RTC_Time.Minute=((BCD_Time.Minute>>4)&0x07)*10+(BCD_Time.Minute&0x0F);
		LCD_WTAppFifo(LCD_MINUTE);
	}	
	
	if(hwI2C_Fifo[2] != BCD_Time.Hour)
	{
		BCD_Time.Hour = hwI2C_Fifo[2];
		RTC_Time.Hour=((BCD_Time.Hour>>4)&0x03)*10+(BCD_Time.Hour&0x0F);
		LCD_WTAppFifo(LCD_HOUR);
	}	
	
	if(hwI2C_Fifo[3] != BCD_Time.Day)
	{
		BCD_Time.Day = hwI2C_Fifo[3];
		RTC_Time.Day=((BCD_Time.Day>>4)&0x03)*10+(BCD_Time.Day&0x0F);
		LCD_WTAppFifo(LCD_DAY);
	}
	
	if(hwI2C_Fifo[5] != BCD_Time.Month)
	{
		BCD_Time.Month = hwI2C_Fifo[5];
		RTC_Time.Month=((BCD_Time.Month>>4)&0x01)*10+(BCD_Time.Month&0x0F);
		LCD_WTAppFifo(LCD_MONTH);
	}	
	
	if(hwI2C_Fifo[6] != BCD_Time.Year)
	{
		BCD_Time.Year = hwI2C_Fifo[6];
		RTC_Time.Year=(BCD_Time.Year>>4)*10+(BCD_Time.Year&0x0F);
		LCD_WTAppFifo(LCD_YEAR);
	}
}

/*****************************************************************************
* Function		: RTC_BCD2DEC
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void RTC_BCD2DEC(void)
{
	RTC_Time.Year=(BCD_Time.Year>>4)*10+(BCD_Time.Year&0x0F);
	RTC_Time.Month=(BCD_Time.Month>>4)*10+(BCD_Time.Month&0x0F);
	RTC_Time.Day=(BCD_Time.Day>>4)*10+(BCD_Time.Day&0x0F);
	RTC_Time.Hour=((BCD_Time.Hour>>4)&0x07)*10+(BCD_Time.Hour&0x0F);
	RTC_Time.Minute=(BCD_Time.Minute>>4)*10+(BCD_Time.Minute&0x0F);
	RTC_Time.Second=((BCD_Time.Second>>4)&0x07)*10+(BCD_Time.Second&0x0F);
}

/*****************************************************************************
* Function		: SysTick_Init
* Description	: Initialization of SysTick timer
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void RTC_DEC2BCD(void) 
{
	BCD_Time.Year=((RTC_Time.Year/10)<<4)+(RTC_Time.Year%10);
	BCD_Time.Month=((RTC_Time.Month/10)<<4)+(RTC_Time.Month%10);
	BCD_Time.Day=((RTC_Time.Day/10)<<4)+(RTC_Time.Day%10);
	BCD_Time.Hour=((RTC_Time.Hour/10)<<4)+(RTC_Time.Hour%10); 
	BCD_Time.Minute=((RTC_Time.Minute/10)<<4)+(RTC_Time.Minute%10);
	BCD_Time.Second=((RTC_Time.Second/10)<<4)+(RTC_Time.Second%10);
}

/*****************************************************************************
* Function		: SetTime
* Description	: Initialization of SysTick timer
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void SetTime(void)
{
	RTC_DEC2BCD();
	hwI2C_Fifo[0] = BCD_Time.Second;
	hwI2C_Fifo[1] = BCD_Time.Minute;
	hwI2C_Fifo[2] = BCD_Time.Hour;
	hwI2C_Fifo[3] = BCD_Time.Day;
	hwI2C_Fifo[5] = BCD_Time.Month;
	hwI2C_Fifo[6] = BCD_Time.Year;	
	WT_I2C_NByte_Val(D8563_ADDR,0x02,7);	
}
/*****************************************************************************
* Function		: ReadTime
* Description	: Initialization of SysTick timer
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void ReadTime(void)
{
	RD_I2C_NByte_Val(D8563_ADDR|0x01,0x02,7);
	BCD_Time.Year	=       hwI2C_Fifo[7];
	BCD_Time.Month	=0x1F & hwI2C_Fifo[6];
	BCD_Time.Day 	=0x3F & hwI2C_Fifo[3];   
	BCD_Time.Hour 	=0x3F & hwI2C_Fifo[2];    
	BCD_Time.Minute =0x7F & hwI2C_Fifo[1];   
	BCD_Time.Second =0x7F & hwI2C_Fifo[0];  
	RTC_BCD2DEC();
}
